//
//  TakePhto.m
//  YiqiMobile
//
//  Created by henry on 13-3-30.
//  Copyright (c) 2013年 henry. All rights reserved.
//



#import "TakePhoto.h"
#import "UIImageUIImageExt.h"
#import "Utility.h"
#import "Config.h"


@implementation TakePhoto
-(UIView *) customOverlay{
    float height=[UIScreen mainScreen].bounds.size.height;
    UIToolbar *toolbar=[[UIToolbar alloc] initWithFrame:CGRectMake(0, height-44, 320, 44)];
    toolbar.barStyle=UIBarStyleBlackOpaque;
    [toolbar addSubview:[Utility customButton:self action:@selector(onclickCameraButton:) normalImage:nil lightedImage:nil frame:CGRectMake(20, 8, 50, 29) title:@"取消" tag:CAMERABUTTONTAG+1]];
    [toolbar addSubview:[Utility customButton:self action:@selector(onclickCameraButton:) normalImage:nil lightedImage:nil frame:CGRectMake(250, 8, 50, 29) title:@"拍照" tag:CAMERABUTTONTAG+2]];
    return toolbar;
}

-(void)  setupCamera:(id) delegate{
    self.delegate=delegate;
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        self.imagePickerController = [[UIImagePickerController alloc] init];
        self.imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
        // Hide the controls:
        self.imagePickerController.showsCameraControls = NO;
        self.imagePickerController.navigationBarHidden = YES;
        self.imagePickerController.wantsFullScreenLayout = YES;
        // self.pickerController.cameraViewTransform = CGAffineTransformScale(self.pickerController.cameraViewTransform, CAMERA_TRANSFORM_X, CAMERA_TRANSFORM_Y);
        self.imagePickerController.cameraViewTransform = CGAffineTransformMakeScale(1.0, 1.0);
        self.imagePickerController.delegate=self;
        // Insert the overlay:
        //self.pickerController.view.userInteractionEnabled=NO ;
        self.imagePickerController.cameraOverlayView=[self customOverlay];
        [_delegate presentViewController:self.imagePickerController animated:YES completion:nil];
    }
 
    
}

-(void) setupLibrary:(id) delegate{
       self.delegate=delegate;
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum]) {
         self.imagePickerController = [[UIImagePickerController alloc] init];
        self.imagePickerController.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
        self.imagePickerController.delegate=self;
        if ([_delegate respondsToSelector:@selector(presentViewController:animated:completion:)]) {
            [_delegate presentViewController:self.imagePickerController animated:NO completion:nil];
        }
    
    }
}

-(IBAction)onclickCameraButton:(UIButton*)sender{
    if (sender.tag==CAMERABUTTONTAG+1) {
        [self cancelDoTakePhoto:nil];
    }else{
        [self doTakePhoto:nil];
    }

}

- (void) cancelDoTakePhoto:(id) sender{
    if ([_delegate respondsToSelector:@selector(dismissViewControllerAnimated:completion:)]) {
           [_delegate dismissModalViewControllerAnimated:YES];
    }
 
}


- (void) doTakePhoto:(id) sender{
        [self.imagePickerController takePicture];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
        UIImage *rawImage=[info  objectForKey:UIImagePickerControllerOriginalImage];
    rawImage=[rawImage imageByScalingAndCroppingForSize:CGSizeMake(320, 568)];
    NSData *imgData = UIImageJPEGRepresentation(rawImage,0.4);
    [_delegate didTakePicture:imgData];
    [self cancelDoTakePhoto:nil];
    
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    [self cancelDoTakePhoto:nil];
    //NSLog(@"拍照取消。。。。");
}



@end
